package com.zhongkai.tools;

import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;




public class MailTool
{
  Logger log = Logger.getLogger(getClass());
  private String host = "smtp.kenfor.com";
  private String encoding = "GB2312";
  private String from = "webmaster@kenfor.com";
  private String pwd = "usermail";
  private boolean isHtmlMode = true;

  //发送邮件 成功返回true,失败返回false
  public boolean sendMail(String to, String subject, String content, String tagHost, String tagFrom, String tagPwd)
  {
	if(tagHost != null && !"".equals(tagHost.trim())){
		this.host = tagHost;
	}
	if(tagFrom != null && !"".equals(tagFrom.trim())){
		this.from = tagFrom;
	}
	if(tagPwd != null && !"".equals(tagPwd.trim())){
		this.pwd = tagPwd;
	}
	boolean result = false;
    if (to == null || to.equals(""))
    {
      log.error("收件人为空，发送失败！");
      return result;
    }
    if (subject == null)
    {
      subject = "";
    }
    if (content == null)
    {
      content = "";
    }

    try
    {
      Properties props = new Properties();
      props.put("mail.host", host);
      props.put("mail.smtp.auth", "true");
      props.put("mail.transport.protocol", "smtp");

      CheckSendEmail auth = new CheckSendEmail(from, pwd);
      Session session = Session.getInstance(props, auth);
      //session.setDebug(true);
      Message msg = new MimeMessage(session);

      Address send = new InternetAddress(from); //发送方地址
      InternetAddress[] address = null;
      address = InternetAddress.parse(to, false); //接受方地址

      msg.setFrom(send);
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(subject);
      msg.setSentDate(new Date());

      if (isHtmlMode == true)
      {
        Multipart mp = new MimeMultipart();
        MimeBodyPart mbp = new MimeBodyPart();
        StringBuffer sb = new StringBuffer(content);
        sb.append("<p>")
            .append("这是系统邮件，请不要直接回复。")
            .append("</p>");
        mbp.setContent(sb.toString(), "text/html;charset=" + encoding);
        mp.addBodyPart(mbp);
        msg.setContent(mp);
      }
      else
      {
        msg.setText(content);
      }
      Transport.send(msg);
      result = true;
      log.info("send mail success!");
    }
    catch (Exception ex)
    {
      result = false;
      log.error(ex.getMessage());
      log.error(ex.toString());
      ex.printStackTrace();
      log.error("send mail failed!");
    }
    return result;
  }
  
//发送邮件 成功返回true,失败返回false
  public boolean sendMail(String to, String subject, String content)
  {
	boolean result = false;
    if (to == null || to.equals(""))
    {
      log.error("收件人为空，发送失败！");
      return result;
    }
    if (subject == null)
    {
      subject = "";
    }
    if (content == null)
    {
      content = "";
    }

    try
    {
      Properties props = new Properties();
      props.put("mail.host", host);
      props.put("mail.smtp.auth", "true");
      props.put("mail.transport.protocol", "smtp");

      CheckSendEmail auth = new CheckSendEmail(from, pwd);
      Session session = Session.getInstance(props, auth);
      //session.setDebug(true);
      Message msg = new MimeMessage(session);

      Address send = new InternetAddress(from); //发送方地址
      InternetAddress[] address = null;
      address = InternetAddress.parse(to, false); //接受方地址

      msg.setFrom(send);
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(subject);
      msg.setSentDate(new Date());

      if (isHtmlMode == true)
      {
        Multipart mp = new MimeMultipart();
        MimeBodyPart mbp = new MimeBodyPart();
        StringBuffer sb = new StringBuffer(content);
        sb.append("<p>")
            .append("这是系统邮件，请不要直接回复。")
            .append("</p>");
        mbp.setContent(sb.toString(), "text/html;charset=" + encoding);
        mp.addBodyPart(mbp);
        msg.setContent(mp);
      }
      else
      {
        msg.setText(content);
      }
      Transport.send(msg);
      result = true;
      log.info("send mail success!");
    }
    catch (Exception ex)
    {
      result = false;
      log.error(ex.getMessage());
      log.error(ex.toString());
      ex.printStackTrace();
      log.error("send mail failed!");
    }
    return result;
  }

  //发送邮件 成功返回true,失败返回false
  public boolean sendDefineMail(String to, String subject, String content)
  {
    boolean result = false;
    if (to == null || to.equals(""))
    {
      log.error("收件人为空，发送失败！");
      return result;
    }
    if (subject == null)
    {
      subject = "";
    }
    if (content == null)
    {
      content = "";
    }

    try
    {
      Properties props = new Properties();
      props.put("mail.host", host);
      props.put("mail.smtp.auth", "true");
      props.put("mail.transport.protocol", "smtp");
      // props.put("mail.smtp.password",pwd);
      CheckSendEmail auth = new CheckSendEmail(from, pwd);
      Session session = Session.getInstance(props, auth);
      session.setDebug(true);
      Message msg = new MimeMessage(session);

      Address send = new InternetAddress(from); //发送方地址
      InternetAddress[] address = null;
      address = InternetAddress.parse(to, false); //接受方地址

      msg.setFrom(send);
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(subject);
      msg.setSentDate(new Date());
      if (isHtmlMode == true)
      {
        Multipart mp = new MimeMultipart();
        MimeBodyPart mbp = new MimeBodyPart();
        StringBuffer sb = new StringBuffer(content);
        sb.append("<p>")
            .append("这是系统邮件，请不要直接回复。")
            .append("</p>");
        mbp.setContent(sb.toString(), "text/html;charset=" + encoding);
        mp.addBodyPart(mbp);
        msg.setContent(mp);
      }
      else
      {
        msg.setText(content);
      }
      //Transport transport = session.getTransport("smtp");
      //transport.connect(host,from,pwd);
      //transport.sendMessage(msg,msg.getAllRecipients());
      Transport.send(msg);
      result = true;
      log.info("send mail success!");
    }
    catch (Exception ex)
    {
      result = false;
      log.error(ex.toString());
      ex.printStackTrace();
      log.error(ex.getMessage());
      log.error("send mail failed!");
    }
    return result;
  }

  //发送邮件 成功返回true,失败返回false
  public boolean sendCustomMail(String to, String subject, String content)
  {
    boolean result = false;
    if (to == null || to.equals(""))
    {
      log.error("收件人为空，发送失败！");
      return result;
    }
    if (subject == null)
    {
      subject = "";
    }
    if (content == null)
    {
      content = "";
    }

    try
    {
      Properties props = new Properties();
      props.put("mail.host", host);
      props.put("mail.transport.protocol", "smtp");
      Session session = Session.getInstance(props, null);
      Message msg = new MimeMessage(session);

      Address send = new InternetAddress(from); //发送方地址
      InternetAddress[] address = null;
      address = InternetAddress.parse(to, false); //接受方地址

      msg.setFrom(send);
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(subject);
      msg.setSentDate(new Date());
      if (isHtmlMode == true)
      {
        Multipart mp = new MimeMultipart();
        MimeBodyPart mbp = new MimeBodyPart();
        StringBuffer sb = new StringBuffer(content);
        sb.append("<p>")
            .append("这是系统邮件，请不要直接回复。")
            .append("</p>");
        mbp.setContent(sb.toString(), "text/html;charset=" + encoding);
        mp.addBodyPart(mbp);
        msg.setContent(mp);
      }
      else
      {
        msg.setText(content);
      }
      Transport.send(msg);
      result = true;
      log.info("send mail success!");
    }
    catch (Exception ex)
    {
      result = false;
      log.error(ex.getMessage());
      log.error(ex.toString());
      ex.printStackTrace();
      log.error("send mail failed!");
    }
    return result;
  }

  public void setHost(String host)
  {
    this.host = host;
  }

  public void setFrom(String from)
  {
    this.from = from;
  }

  public void setEncoding(String encoding)
  {
    this.encoding = encoding;
  }

  public void setIsHtmlMode(boolean isHtmlMode)
  {
    this.isHtmlMode = isHtmlMode;
  }

  public void setPwd(String pwd)
  {
    this.pwd = pwd;
  }
}
